package com.mou.controller.user;

import com.mou.context.BaseContext;
import com.mou.dto.ChatBoxDTO;
import com.mou.dto.MessageDTO;
import com.mou.dto.MessageQueryDTO;
import com.mou.result.Result;
import com.mou.service.ChatService;
import com.mou.vo.ChatBoxVO;
import com.mou.vo.MessageVO;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController("userChatController")
@RequestMapping("/user/chat")
@RequiredArgsConstructor
public class ChatController {

    private final ChatService chatService;

    // 发送消息
    @PostMapping("/sendMessages")
    public Result<List<Integer>> sendMessage(@Valid @RequestBody MessageDTO messageDTO) {
        List<Integer> messageId = chatService.sendMessage(messageDTO);
        return Result.success(messageId);
    }

    // 获取聊天记录
    @PostMapping("/getMessages")
    public Result<List<MessageVO>> getMessages(@RequestBody MessageQueryDTO messageQueryDTO) {
        List<MessageVO> messages = chatService.getMessages(messageQueryDTO);
        return Result.success(messages);
    }

    // 消息已读确认
    @PutMapping("/readMessages/{id}")
    public Result<String> markAsRead(@PathVariable Integer id) {
        chatService.markMessageAsRead(id);
        return Result.success();
    }

    @PostMapping("/addChatBox")
    public Result<String> addChatBox(@RequestBody ChatBoxDTO chatBoxDTO) {
        chatService.addChatBox(chatBoxDTO);
        return Result.success();
    }

    @GetMapping("/getChatBox")
    public Result<List<ChatBoxVO>> getChatBox() {
        log.info("当前登录Id:"+ BaseContext.getCurrentId());
        List<ChatBoxVO> chatBoxVOS = chatService.getChatBox();
        return Result.success(chatBoxVOS);
    }
}